import { Body, Controller, Get, Post,  Param, Delete, HttpCode, HttpStatus, Query,ParseIntPipe, Put } from '@nestjs/common';
import { FormService , PublishProps} from './form.service';
import { Public } from '@/modules/auth/decorators/public.decorator';


interface UpdateType{
  type: 'rocket' | 'collect' | 'report'
  userId: string
}

@Controller('/api/publish')
export class FormController {
  constructor(private readonly formService: FormService) {}

  @Post('create')
  async submitForm(@Body() formData: any) {
    try {
         console.log('是否可以创建', formData);
         const data = await this.formService.addForm(formData);
      if (data === undefined) {
        return {
          code: 200,
          data: [],
          message: 'ok',
        };
      } else {
        return {
          code: 500,
          data: [],
          message: data
        };
      }
    } catch (error) {
      console.log(error);
    }
  }





  @Public()
  @HttpCode(HttpStatus.OK)
  @Get('page')
  async publishPage(
    @Query("pageNumber",ParseIntPipe ) pageNumber: number,
    @Query("pageSize",ParseIntPipe ) pageSize: number,
    @Query("role" ) role: 'driver' | 'passenger',
    @Query("des" ) des: 'startLocation' | 'endLocation' | 'all',
    @Query("search" ) search: string,
    @Query("map" ) map: PublishProps['map'],
  ) : Promise<any>{
    try {
      const d = await this.formService.publishPage({ pageNumber, pageSize, search, role, des, map});
      console.log(d)
      return d;
      // return;
      // const existUser = await this.userService.findOneByName(formData.username);
      // if(existUser === null){ // 此时不存在该用户可以新增
      //   const data = await this.userService.addUser(user);
      //   console.log('新增', data);
      //   if (data === undefined) {
      //     return {
      //       code: 0,
      //       data: [],
      //       message: 'ok',
      //     };
      //   } 
      //   if (data && data.code === 200) {
      //     return {
      //       code: 0,
      //       message: data.message || data.message.driverError.sqlMessage,
      //     };
      //   } else {
      //     return {
      //       code: data.code,
      //       message: data.message.driverError.sqlMessage,
      //     };
      //   }
      // } else { // 该用户已注册
      //   return {
      //     code: 1000,
      //     data: user,
      //     message:"该用户已注册"
      //   }
      // }
    } catch (error) {
      console.log('error', error);
      return {
        code: 500,
        message: error,
      };
    }
  }

  


  @Put('putReportOrCollectOrRocket/:publish_id')
  async putReportOrCollectOrRocket(
    @Param('publish_id') publish_id: string,
    @Body() data: UpdateType,
  ): Promise<any> {
    try {
      const upateAfter = await this.formService.upateTypeCount(
        publish_id,
        data,
      );
      if (upateAfter) {
        const { password, ...otherUserData } = upateAfter;
        return {
          code: 200,
          data: otherUserData,
          message: 'ok',
        };
      } else {
        return {
          code: 1000,
          data: null,
          message: 'error',
        };
      }
    } catch (error) {
      console.log('修改后报错', error);
      return {
        code: 500,
        data: null,
        message: error,
      };
    }
  }


}
